import { useRequest } from 'umi';
import { requestFunUpload, requestFunComponFiex, getUserID, getPosIDs, getUserDepID, getCompanyID, requestFunHtml } from '@/utils';
import { getBaseUrl } from '@/services/urlconst';
import moment from 'moment';

const baseUrl = getBaseUrl();
export const ExpertsUpdateService = () => {
  // 上传证书/头像
  const uploadExpertsCertificateService = () => {
    const request = useRequest(
      (requestParams?: any) => {
        const reqUrl = `${baseUrl}/expertInfoController/uploadFile`;
        return requestFunUpload('POST', reqUrl, requestParams);
      },
      {
        manual: true,
      },
    );
    return {
      uploadExpertsCertificateRun: request.run,
      uploadExpertsCertificateLoading: request.loading,
    };
  };

  // 添加专家信息
  const addExpertsService = () => {
    const request = useRequest(
      (requestParams?: Record<string, any>) => {
        const reqUrl = `${baseUrl}/expertInfoController/addExpert`;
        const params: Record<string, any> = {
          createrID: getUserID(),
          createPositionID: getPosIDs(),
          createDepartmentID: getUserDepID(),
          createCompanyID: getCompanyID(),
          isOpen: '1',
          ...requestParams,
        };
        if (params?.birthday) params.birthday = moment(params?.birthday)?.format('YYYY-MM-DD HH:mm:ss');
        return requestFunComponFiex('POST', reqUrl, params);
      },
      {
        manual: true,
      },
    );
    return {
      addExpertsRun: request.run,
      addExpertsLoading: request.loading,
    };
  };

  // 编辑专家信息
  const editExpertsService = () => {
    const request = useRequest(
      (requestParams?: Record<string, any>) => {
        const reqUrl = `${baseUrl}/expertInfoController/editExpert`;
        const params: Record<string, any> = {
          ...requestParams,
          modifyerID: getUserID(),
          modifyPositionID: getPosIDs(),
          modifyDepartmentID: getUserDepID(),
          modifyCompanyID: getCompanyID(),
        };
        if (params?.birthday) params.birthday = moment(params?.birthday)?.format('YYYY-MM-DD HH:mm:ss');
        return requestFunComponFiex('PUT', reqUrl, params);
      },
      {
        manual: true,
      },
    );
    return {
      editExpertsRun: request.run,
      editExpertsLoading: request.loading,
    };
  };

  // 获取专家信息
  const findExpertsService = () => {
    const request = useRequest(
      (requestParams?: Record<string, any>) => {
        const reqUrl = `${baseUrl}/expertInfoController/getExpertDetails/${requestParams}`;
        // const reqUrl = `${baseUrl}/expertInfoController/getExpertDetails/8364571071473078272`;
        return requestFunComponFiex('GET', reqUrl, {});
      },
      {
        manual: true,
      },
    );
    return {
      findExpertsRun: request.run,
      findExpertsLoading: request.loading,
    };
  };

  // 添加证书
  const addExpertsCertificateService = () => {
    const request = useRequest(
      (requestParams?: Record<string, any>) => {
        const reqUrl = `${baseUrl}/expertInfoController/addQualify`;
        const params = {
          createrID: getUserID(),
          createPositionID: getPosIDs(),
          createDepartmentID: getUserDepID(),
          createCompanyID: getCompanyID(),
          ...requestParams,
        };
        return requestFunComponFiex('POST', reqUrl, params);
      },
      {
        manual: true,
      },
    );
    return {
      addExpertsCertificateRun: request.run,
      addExpertsCertificateLoading: request.loading,
    };
  };

  // 编辑证书
  const editExpertsCertificateService = () => {
    const request = useRequest(
      (requestParams?: Record<string, any>) => {
        const reqUrl = `${baseUrl}/expertInfoController/editQualify`;
        const params = {
          createrID: getUserID(),
          createPositionID: getPosIDs(),
          createDepartmentID: getUserDepID(),
          createCompanyID: getCompanyID(),
          ...requestParams,
        };
        return requestFunComponFiex('PUT', reqUrl, params);
      },
      {
        manual: true,
      },
    );
    return {
      editExpertsCertificateRun: request.run,
      editExpertsCertificateLoading: request.loading,
    };
  };

  // 删除证书
  const delExpertsCertificateService = (callback) => {
    const request = useRequest(
      (requestParams?: Record<string, any>) => {
        const reqUrl = `${baseUrl}/expertInfoController/deleteQualify`;
        const params = {
          createrID: getUserID(),
          createPositionID: getPosIDs(),
          createDepartmentID: getUserDepID(),
          createCompanyID: getCompanyID(),
          ...requestParams,
        };
        return requestFunComponFiex('DELETE', reqUrl, params);
      },
      {
        manual: true,
        onSuccess: callback,
      },
    );
    return {
      delExpertsCertificateRun: request.run,
      delExpertsCertificateLoading: request.loading,
    };
  };

  // 导出专家
  const exportExpertService = () => {
    const request = useRequest(
      (payload: any) => {
        let reqUrl = `${baseUrl}/expertInfoController/export`;
        return requestFunHtml('POST', reqUrl, { ...payload });
      },
      {
        manual: true,
      },
    );
    return {
      exportExpertRun: request.run,
      exportExpertLoading: request.loading,
    };
  };

  return {
    addExpertsService,
    findExpertsService,
    editExpertsService,
    addExpertsCertificateService,
    editExpertsCertificateService,
    uploadExpertsCertificateService,
    delExpertsCertificateService,
    exportExpertService,
  };
};
